#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <time.h>

using namespace std;

class Solution
{
public:
    vector<int> sortArray(vector<int>& nums)
    {
        srand(time(nullptr));
        qsort(nums, 0, nums.size() - 1);
        return nums;
    }
    void qsort(vector<int>& nums, int l, int r)
    {
        if (l >= r) return;
        int i = l;
        int left = l - 1, right = r + 1;
        int key = nums[rand() % (r - l + 1) + l];
        while (i < right)
        {
            if (nums[i] < key)
                swap(nums[++left], nums[i++]);
            else if (nums[i] == key)
                i++;
            else
                swap(nums[--right], nums[i]);
        }
        qsort(nums, l, left);
        qsort(nums, right, r);
    }
};

class Solution
{
public:
    void sortColors(vector<int>& nums)
    {
        int i = 0, left = -1, right = nums.size();
        while (i < right)
        {
            if (nums[i] == 0) swap(nums[++left], nums[i++]);
            else if (nums[i] == 1) i++;
            else swap(nums[--right], nums[i]);
        }
    }
};